<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of pdist2</title>
  <meta name="keywords" content="pdist2">
  <meta name="description" content="Calculates the distance between sets of vectors.">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../m2html.css">
</head>
<body>
<a name="_top"></a>
<!-- menu.html classify -->
<h1>pdist2
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>Calculates the distance between sets of vectors.</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>function D = pdist2( X, Y, metric ) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre class="comment"> Calculates the distance between sets of vectors.

 Let X be an m-by-p matrix representing m points in p-dimensional space
 and Y be an n-by-p matrix representing another set of points in the same
 space. This function computes the m-by-n distance matrix D where D(i,j)
 is the distance between X(i,:) and Y(j,:).  This function has been
 optimized where possible, with most of the distance computations
 requiring few or no loops.

 The metric can be one of the following:

 'euclidean' / 'sqeuclidean':
   Euclidean / SQUARED Euclidean distance.  Note that 'sqeuclidean'
   is significantly faster.

 'chisq'
   The chi-squared distance between two vectors is defined as:
    d(x,y) = sum( (xi-yi)^2 / (xi+yi) ) / 2;
   The chi-squared distance is useful when comparing histograms.

 'cosine'
   Distance is defined as the cosine of the angle between two vectors.

 'emd'
   Earth Mover's Distance (EMD) between positive vectors (histograms).
   Note for 1D, with all histograms having equal weight, there is a simple
   closed form for the calculation of the EMD.  The EMD between histograms
   x and y is given by the sum(abs(cdf(x)-cdf(y))), where cdf is the
   cumulative distribution function (computed simply by cumsum).

 'L1'
   The L1 distance between two vectors is defined as:  sum(abs(x-y));


 USAGE
  D = pdist2( X, Y, [metric] )

 INPUTS
  X        - [m x p] matrix of m p-dimensional vectors
  Y        - [n x p] matrix of n p-dimensional vectors
  metric   - ['sqeuclidean'], 'chisq', 'cosine', 'emd', 'euclidean', 'L1'

 OUTPUTS
  D        - [m x n] distance matrix

 EXAMPLE
  % simple example where points cluster well
  [X,IDX] = demoGenData(100,0,5,4,10,2,0);
  D = pdist2( X, X, 'sqeuclidean' );
  distMatrixShow( D, IDX );
  % comparison to pdist
  n=500; d=200; r=100; X=rand(n,d);
  tic, for i=1:r, D1 = pdist( X, 'euclidean' ); end, toc
  tic, for i=1:r, D2 = pdist2( X, X, 'euclidean' ); end, toc
  D1=squareform(D1); del=D1-D2; sum(abs(del(:)))
 
 See also pdist, <a href="distMatrixShow.html" class="code" title="function [D, Dsm] = distMatrixShow( D, IDX, show )">distMatrixShow</a>

 Piotr's Computer Vision Matlab Toolbox      Version 2.52
 Copyright 2014 Piotr Dollar.  [pdollar-at-gmail.com]
 Licensed under the Simplified BSD License [see external/bsd.txt]</pre></div>





<!-- Start of Google Analytics Code -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-4884268-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
<!-- end of Google Analytics Code -->

<hr><address>Generated by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" target="_parent">m2html</a></strong> &copy; 2003</address>
</body>
</html>
